package com.zhonghui.controller;

import com.zhonghui.common.core.result.FwResult;
import com.zhonghui.dto.AddressCreateDto;
import com.zhonghui.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 地址信息
 * @author: FL
 * @create: 2024 2024/5/6 8:17
 */
@RestController
@Api(value = "地址信息")
@Slf4j
@Validated
public class AddressController {
  @Autowired
  AddressService addressService;

 /**
  * 获取所有地址
  */
 @ApiOperation("获取所有地址")
 @PostMapping("/address")
 public FwResult getAllAddress(){
  return addressService.getAllAddress();
 }

    /**
     * 创建地址
     */
    @ApiOperation("创建地址")
    @PostMapping("/address/add")
    public FwResult createAddress(@RequestBody AddressCreateDto addressCreateDto){
        return addressService.createAddress(addressCreateDto);
    }

    /**
     * 修改地址
     */
    @PutMapping("/address/{id}")
    @CachePut(value = "address",key = "#id")
    public FwResult updateAddress(@RequestBody AddressCreateDto addressCreateDto, @PathVariable Long id){
        return addressService.updateAddress(addressCreateDto,id);
    }


    /**
     * 根据id删除地址
     */
    @ApiOperation("根据id删除地址")
    @DeleteMapping("/address/{id}")
    @CacheEvict(value = "address",key = "#id")
    public FwResult deleteAddress(@PathVariable Long id){
        return addressService.deleteAddress(id);
    }
}
